package com.smart.manager.member.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.member.entities.bodys.RMemberMaterialProps;
import com.smart.manager.member.entities.enums.PropsKind;
import com.smart.manager.member.entities.models.MemberMaterialProps;
import com.smart.manager.member.entities.models.query.QMemberMaterialProps;
import com.smart.manager.product.entities.models.PropsCombo;
import com.smart.manager.product.entities.models.query.QPropsCombo;
import com.smart.manager.product.entities.views.PropsComboView;
import com.smart.manager.product.entities.views.PropsServeView;
import com.smart.manager.product.entities.views.query.QPropsComboView;
import com.smart.manager.product.entities.views.query.QPropsServeView;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;


/**
 * 玩家背包
 *
 * @author leo
 * @date 2023/10/04
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class MemberMaterialPropsRepository {

    public Page<MemberMaterialProps> pageList(RMemberMaterialProps.SearchBody search) {
        QMemberMaterialProps query = new QMemberMaterialProps();
        Opt.ofBlankAble(search.getMember()).ifPresent(x -> query.member.nickName.contains(x));
        Opt.ofBlankAble(search.getKind()).ifPresent(x -> query.kind.eq(x));
        query.order().number.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<MemberMaterialProps>().load(query.findPagedList());
    }


    public void add(RMemberMaterialProps.EditBody body) {
        Optional<MemberMaterialProps> empty = new QMemberMaterialProps().member.eq(body.getMember()).kind.eq(body.getKind()).code.eq(body.getCode()).setMaxRows(1).findOneOrEmpty();
        //如果存在就堆叠 , 如果不存在就新增
        if (empty.isEmpty()) {
            BeanUtil.copyProperties(body, MemberMaterialProps.class).save();
        } else {
            MemberMaterialProps props = empty.get();
            props.setNumber(props.getNumber() + body.getNumber()).update();
        }
    }


    public void update(Long id, RMemberMaterialProps.EditBody body) {
        QMemberMaterialProps query = new QMemberMaterialProps();
        MemberMaterialProps record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<MemberMaterialProps> list = new QMemberMaterialProps().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        return DB.deleteAll(list);
    }

    /**
     * @param id 会员id
     * @return {@link List}<{@link MemberMaterialProps}>
     */
    public List<MemberMaterialProps> getMemberList(Long id){
        return new QMemberMaterialProps().member.id.eq(id).findList();
    }

}






